package owg.util.opengl.models;

import java.nio.ShortBuffer;
import java.nio.IntBuffer;

import com.jogamp.opengl.*;

import owg.util.Calc;
import owg.util.opengl.EGL;



/**
 * This object can be used to build and store Vertex Arrays in a conceptually simple and general way.
 * Is an implementation of High Performance Renderable.
 */
public class VAS extends HPRS
{
	ShortBuffer vBuf,cBuf,nBuf,tcBuf,tc2Buf;
	
	IntBuffer iBuf;
	
	/**Whether the direct buffer is outdated and needs to be replaced with the java array*/
	private boolean vOutdated, iOutdated;
	/**Construct a new object which uses the vertex data from dataSrc
	 * Index data must be specified.*/
	public VAS(VAS dataSrc, int faceMode, String name)
	{
		super(name, faceMode, dataSrc.numVertexComponents, dataSrc.useNormal, dataSrc.numColorComponents, dataSrc.numTexCoordComponents, dataSrc.numTexCoordComponents2);
		
		vData = dataSrc.vData;
		vBuf = dataSrc.vBuf;
		
		iData = dataSrc.iData;
		iBuf = dataSrc.iBuf;

		//Is overwritten if indices are added or setPrepackedIData is called.
		iBuildMode = BUILD_SHARED_BUFFER;
		
		//Duplicates of vBuf with different offsets
		nBuf = dataSrc.nBuf;
		cBuf = dataSrc.cBuf;
		tcBuf = dataSrc.tcBuf;
		tc2Buf = dataSrc.tc2Buf;

		//Is overwritten if vertices are added or setPrepackedVData is called.
		vBuildMode = BUILD_SHARED_BUFFER;
		
		vOutdated = false;
		iOutdated = false;
	}
	/**Construct an empty object with the given settings*/
	public VAS(String name, int faceMode, int nVertexComps, boolean useNormal, int nColorComps, 
		int nTexCoords, int nTexCoords2)
		{
		super(name, faceMode, (byte)nVertexComps, useNormal, (byte)nColorComps, (byte)nTexCoords, (byte)nTexCoords2);
		}
	/**Construct an empty object with the given settings*/
	public VAS(int faceMode, boolean useNormal, boolean useColor, boolean useAlpha, 
		boolean useTexCoord,boolean use3DTexCoord, boolean useTexCoord2, boolean use3DTexCoord2, String name)
		{
		this(name, faceMode, 3, useNormal, (useColor?(useAlpha?4:3):0), (useTexCoord?(use3DTexCoord?3:2):0), (useTexCoord2?(use3DTexCoord2?3:2):0));
		}
	@Override
	public void requestVUpdate()
	{
		vOutdated = true;
	}
	
	@Override
	public void requestIUpdate()
	{
		iOutdated = true;
	}
	@Override
	protected void endImpl()
	{
		if(vBuildMode == BUILD_ARRAYLIST || vBuildMode == BUILD_PREPACKED)
		{
			vBuf = egl.asBuffer(vData);
			if(vBuf.capacity() > 0)
			{
				if(useNormal)
				{
					nBuf = vBuf.duplicate();
					nBuf.position(normalPointer);
				}
				if(numColorComponents > 0)
				{
					cBuf = vBuf.duplicate();
					cBuf.position(colorPointer);
				}
				if(numTexCoordComponents > 0)
				{
					tcBuf = vBuf.duplicate();
					tcBuf.position(texCoordPointer);
				}
				if(numTexCoordComponents2 > 0)
				{
					tc2Buf = vBuf.duplicate();
					tc2Buf.position(texCoordPointer2);
				}
			}
		}
		if(iBuildMode == BUILD_ARRAYLIST || iBuildMode == BUILD_PREPACKED)
			iBuf = egl.asBuffer(iData);
		//Note: Shared buffers in vertex arrays do not need any special management since they must be garbage collected normally.
		vOutdated = false;
		iOutdated = false;
	}
	
	
	/* (non-Javadoc)
	 * @see graphics.HPR#render()
	 */
	@Override
	public void render() 
	{
		if(vBuf.capacity() == 0)
			return;
		GL2 gl = (GL2) egl.getGL();
		
		if(vOutdated)
		{
			vBuf.put(vData);
			vBuf.rewind();
			vOutdated = false;
		}
		if(iOutdated)
		{
			iBuf.put(iData);
			iBuf.rewind();
			iOutdated = false;
		}
		gl.glVertexPointer(numVertexComponents, GL.GL_SHORT, vertexStride*Calc.bytesPerShort, vBuf);
		if(useNormal)
			gl.glNormalPointer(GL.GL_SHORT, vertexStride*Calc.bytesPerShort, nBuf);
		if(numColorComponents > 0)
			gl.glColorPointer(numColorComponents, GL.GL_UNSIGNED_SHORT, vertexStride*Calc.bytesPerShort, cBuf);
		if(numTexCoordComponents > 0)
			gl.glTexCoordPointer(numTexCoordComponents, GL.GL_SHORT, vertexStride*Calc.bytesPerShort, tcBuf);
		if(numTexCoordComponents2 > 0)
			gl.glTexCoordPointer(numTexCoordComponents2, GL.GL_SHORT, vertexStride*Calc.bytesPerShort, tc2Buf);
		
		gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
		if(useNormal)
			gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
		if(numColorComponents > 0)
			gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
		if(numTexCoordComponents > 0)
			gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
		if(numTexCoordComponents2 > 0)
		{
			gl.glClientActiveTexture(GL2.GL_TEXTURE1);
			gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
			gl.glClientActiveTexture(GL2.GL_TEXTURE0);
		}
		
		int end = 0;
		for(int i=0; i<faceModeEndIndices.size(); i++)
		{
			int currentFaceMode = faceModes.get(i);
			int start = end;
			end = faceModeEndIndices.get(i);
			if(renderLimit != -1) 
			{
				if(start >= renderLimit)
					break;
				end = Math.min(end, renderLimit);
			}
			int length = end-start;
			//Select position in index buffer
			iBuf.position(start);
			gl.glDrawElements(currentFaceMode,length, GL2.GL_UNSIGNED_INT, iBuf);
		}
		iBuf.rewind();
		
		gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
		if(useNormal)
			gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
		if(numColorComponents > 0)
			gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
		if(numTexCoordComponents > 0)
			gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
		if(numTexCoordComponents2 > 0)
		{
			gl.glClientActiveTexture(GL2.GL_TEXTURE1);
			gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
			gl.glClientActiveTexture(GL2.GL_TEXTURE0);
		}
	}
	
	/* (non-Javadoc)
	 * @see graphics.HPR#dispose(com.jogamp.opengl.GL)
	 */
	@Override
	public void dispose(EGL<?> gl)  
	{
		this.vBuf = null;
		this.iBuf = null;
	}
}
